{% extends 'base.html' %}

{% block title %}{{ warehouse.name }} - 仓库详情 - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
                <li class="breadcrumb-item"><a href="{% url 'assets:warehouse_list' %}">仓库管理</a></li>
                <li class="breadcrumb-item active" aria-current="page">{{ warehouse.name }}</li>
            </ol>
        </nav>
        
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2><i class="fas fa-warehouse me-2"></i>{{ warehouse.name }}</h2>
            <div class="btn-group">
                <a href="{% url 'assets:warehouse_update' warehouse.pk %}" class="btn btn-primary">
                    <i class="fas fa-edit me-1"></i>编辑仓库
                </a>
                <button type="button" class="btn btn-outline-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown">
                    <span class="visually-hidden">更多操作</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="{% url 'assets:consumable_list' %}?warehouse={{ warehouse.pk }}">
                        <i class="fas fa-boxes me-2"></i>查看仓库耗材
                    </a></li>
                    <li><a class="dropdown-item" href="{% url 'assets:inventory_log_list' %}?warehouse={{ warehouse.pk }}">
                        <i class="fas fa-history me-2"></i>查看库存记录
                    </a></li>
                    {% if warehouse.location %}
                    <li><a class="dropdown-item" href="{% url 'assets:location_detail' warehouse.location.pk %}">
                        <i class="fas fa-map-marker-alt me-2"></i>查看所在位置
                    </a></li>
                    {% endif %}
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-danger" href="{% url 'assets:warehouse_delete' warehouse.pk %}" onclick="return confirm('确定要删除仓库 {{ warehouse.name }} 吗？')">
                        <i class="fas fa-trash me-2"></i>删除仓库
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="{% url 'assets:warehouse_list' %}">
                        <i class="fas fa-arrow-left me-2"></i>返回列表
                    </a></li>
                </ul>
            </div>
        </div>

        <div class="row">
            <!-- 基本信息 -->
            <div class="col-md-8">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-info-circle me-2"></i>基本信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">仓库名称:</td>
                                        <td><strong>{{ warehouse.name }}</strong></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">所在位置:</td>
                                        <td>
                                            {% if warehouse.location %}
                                                <div class="d-flex align-items-center">
                                                    <i class="fas fa-map-marker-alt me-1 text-success"></i>
                                                    <a href="{% url 'assets:location_detail' warehouse.location.pk %}" class="text-decoration-none">
                                                        {{ warehouse.location.name }}
                                                    </a>
                                                </div>
                                            {% else %}
                                                <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">管理员:</td>
                                        <td>
                                            {% if warehouse.manager %}
                                                <div class="d-flex align-items-center">
                                                    <i class="fas fa-user me-1 text-info"></i>
                                                    <a href="{% url 'assets:user_detail' warehouse.manager.pk %}" class="text-decoration-none">
                                                        {{ warehouse.manager.get_full_name|default:warehouse.manager.username }}
                                                    </a>
                                                </div>
                                            {% else %}
                                                <span class="text-muted">未指定</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">状态:</td>
                                        <td>
                                            <span class="badge bg-success">
                                                <i class="fas fa-check me-1"></i>活跃
                                            </span>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">创建时间:</td>
                                        <td>
                                            <i class="fas fa-calendar me-1 text-muted"></i>{{ warehouse.created_at|date:"Y-m-d H:i" }}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">更新时间:</td>
                                        <td>
                                            <i class="fas fa-clock me-1 text-muted"></i>{{ warehouse.updated_at|date:"Y-m-d H:i" }}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">耗材种类:</td>
                                        <td>
                                            <i class="fas fa-boxes me-1 text-muted"></i>{{ total_consumable_types }} 种
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">库存记录:</td>
                                        <td>
                                            <i class="fas fa-history me-1 text-muted"></i>{{ total_inventory_logs }} 条
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        
                        {% if warehouse.description %}
                        <div class="mt-3">
                            <h6 class="text-muted">描述:</h6>
                            <p class="mb-0">{{ warehouse.description }}</p>
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <!-- 仓库耗材 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-boxes me-2"></i>仓库耗材
                            {% if total_consumables > 10 %}
                                <a href="{% url 'assets:consumable_list' %}?warehouse={{ warehouse.pk }}" class="btn btn-outline-primary btn-sm float-end">
                                    查看全部 {{ total_consumables }} 种耗材
                                </a>
                            {% endif %}
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if consumables %}
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead class="table-light">
                                        <tr>
                                            <th>耗材名称</th>
                                            <th>耗材编码</th>
                                            <th>类别</th>
                                            <th>当前库存</th>
                                            <th>最低库存</th>
                                            <th>单价</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for consumable in consumables %}
                                        <tr>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <i class="fas fa-box me-2 text-primary"></i>
                                                    <strong>{{ consumable.name }}</strong>
                                                </div>
                                            </td>
                                            <td><code>{{ consumable.code }}</code></td>
                                            <td>
                                                {% if consumable.category %}
                                                    <span class="badge bg-info">{{ consumable.category.name }}</span>
                                                {% else %}
                                                    <span class="text-muted">-</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <span class="{% if consumable.quantity <= consumable.min_quantity %}text-danger{% else %}text-success{% endif %}">
                                                    {{ consumable.quantity }} {{ consumable.unit }}
                                                </span>
                                            </td>
                                            <td>{{ consumable.min_quantity }} {{ consumable.unit }}</td>
                                            <td>¥{{ consumable.purchase_price }}</td>
                                            <td>
                                                {% if consumable.quantity <= consumable.min_quantity %}
                                                    <span class="badge bg-danger">库存不足</span>
                                                {% elif consumable.quantity <= consumable.min_quantity|add:10 %}
                                                    <span class="badge bg-warning">库存偏低</span>
                                                {% else %}
                                                    <span class="badge bg-success">库存充足</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <a href="{% url 'assets:consumable_detail' consumable.pk %}" class="btn btn-outline-info btn-sm" title="查看耗材详情">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        {% else %}
                            <div class="text-muted text-center py-3">
                                <i class="fas fa-boxes fa-2x mb-2"></i>
                                <p class="mb-0">该仓库暂无耗材</p>
                            </div>
                        {% endif %}
                    </div>
                </div>
                
                <!-- 库存记录 -->
                {% if inventory_logs %}
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-history me-2"></i>最近库存记录
                            {% if total_inventory_logs > 10 %}
                                <a href="{% url 'assets:inventory_log_list' %}?warehouse={{ warehouse.pk }}" class="btn btn-outline-primary btn-sm float-end">
                                    查看全部 {{ total_inventory_logs }} 条记录
                                </a>
                            {% endif %}
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>耗材</th>
                                        <th>操作类型</th>
                                        <th>数量变化</th>
                                        <th>操作人</th>
                                        <th>时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for log in inventory_logs %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-box me-2 text-muted"></i>
                                                {{ log.consumable.name }}
                                            </div>
                                        </td>
                                        <td>
                                            {% if log.operation_type == 'in' %}
                                                <span class="badge bg-success">入库</span>
                                            {% elif log.operation_type == 'out' %}
                                                <span class="badge bg-danger">出库</span>
                                            {% elif log.operation_type == 'adjust' %}
                                                <span class="badge bg-warning">调整</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if log.operation_type == 'in' %}
                                                <span class="text-success">+{{ log.quantity }}</span>
                                            {% elif log.operation_type == 'out' %}
                                                <span class="text-danger">-{{ log.quantity }}</span>
                                            {% else %}
                                                {{ log.quantity }}
                                            {% endif %}
                                            {{ log.consumable.unit }}
                                        </td>
                                        <td>
                                            {% if log.operator %}
                                                <i class="fas fa-user me-1 text-info"></i>{{ log.operator.get_full_name|default:log.operator.username }}
                                            {% else %}
                                                <span class="text-muted">系统</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ log.created_at|date:"m-d H:i" }}</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                {% endif %}
            </div>

            <!-- 统计信息 -->
            <div class="col-md-4">
                <!-- 仓库状态卡片 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-chart-pie me-2"></i>仓库概览
                        </h5>
                    </div>
                    <div class="card-body text-center">
                        <div class="mb-3">
                            <div class="alert alert-info">
                                <i class="fas fa-warehouse fa-2x mb-2"></i>
                                <h5>仓库信息</h5>
                                <p class="mb-0">{{ warehouse.name }}</p>
                            </div>
                        </div>
                        
                        <div class="row text-center">
                            <div class="col-4">
                                <h4 class="text-primary">{{ total_consumables }}</h4>
                                <small class="text-muted">耗材种类</small>
                            </div>
                            <div class="col-4">
                                <h4 class="text-warning">{{ low_stock_count }}</h4>
                                <small class="text-muted">库存不足</small>
                            </div>
                            <div class="col-4">
                                <h4 class="text-info">{{ total_inventory_logs }}</h4>
                                <small class="text-muted">库存记录</small>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 快速操作 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-bolt me-2"></i>快速操作
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="{% url 'assets:consumable_create' %}" class="btn btn-success btn-sm">
                                <i class="fas fa-plus me-2"></i>添加耗材
                            </a>
                            <a href="{% url 'assets:consumable_list' %}?warehouse={{ warehouse.pk }}" class="btn btn-info btn-sm">
                                <i class="fas fa-boxes me-2"></i>查看仓库耗材
                            </a>
                            <a href="{% url 'assets:inventory_log_list' %}?warehouse={{ warehouse.pk }}" class="btn btn-warning btn-sm">
                                <i class="fas fa-history me-2"></i>查看库存记录
                            </a>
                            {% if warehouse.location %}
                            <a href="{% url 'assets:location_detail' warehouse.location.pk %}" class="btn btn-outline-primary btn-sm">
                                <i class="fas fa-map-marker-alt me-2"></i>查看所在位置
                            </a>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <!-- 统计信息 -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-chart-bar me-2"></i>统计信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-borderless table-sm">
                            <tr>
                                <td class="text-muted">仓库ID:</td>
                                <td>
                                    <code>{{ warehouse.pk }}</code>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">耗材种类:</td>
                                <td>
                                    <i class="fas fa-boxes me-1 text-muted"></i>
                                    {{ total_consumables }} 种
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">库存不足:</td>
                                <td>
                                    <i class="fas fa-exclamation-triangle me-1 text-warning"></i>
                                    {{ low_stock_count }} 种
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">库存记录:</td>
                                <td>
                                    <i class="fas fa-history me-1 text-muted"></i>
                                    {{ total_inventory_logs }} 条
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">创建时间:</td>
                                <td>
                                    <i class="fas fa-calendar me-1 text-muted"></i>
                                    {{ warehouse.created_at|date:"Y-m-d" }}
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}